package Controller;

import Bean.MindMap;
import Bean.MindMapLine;
import Bean.MindMapNode;
import Bean.Position;
import Utils.FileUtils;
import Utils.LayoutUtils;
import Utils.PropertiesUtils;
import Utils.TreeUtils;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Orientation;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.*;
import javafx.scene.control.Menu;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import javax.imageio.ImageIO;
import java.io.File;
import java.net.URL;
import java.util.*;

public class MainInterface extends Application {

    //创建空白导图选项
    @FXML
    public Menu createNewMap;

    //打开导图选项
    @FXML
    public Menu openMap;

    //保存按钮
    @FXML
    public ImageView storeMap = new ImageView();

    //创建节点按钮
    @FXML
    public ImageView createNode = new ImageView();

    //删除节点按钮
    @FXML
    public ImageView deleteNode = new ImageView();

    //上一步按钮
    @FXML
    public ImageView pre = new ImageView();

    //下一步按钮
    @FXML
    public ImageView next = new ImageView();

    //导出为图片按钮
    @FXML
    public ImageView imageToJPG = new ImageView();

    //右布局按钮
    @FXML
    public ImageView rightLayout = new ImageView();

    //自动布局按钮
    @FXML
    public ImageView autoLayout = new ImageView();

    //左布局按钮
    @FXML
    public ImageView leftLayout = new ImageView();

    //左侧树组件
    @FXML
    public TreeView list;

    //选项卡组件
    @FXML
    public TabPane tabPane;

    //绘制面板组件
    @FXML
    public Pane mainPane;

    @FXML
    public Pane dialog;

    @FXML
    public Pane imageDialog;

    private Stage primaryStage;

    private Stage dialogStage;

    private Pane dialogPane = new Pane();

    private Label dialogMess = new Label();


    //水平滚动条
    ScrollBar horizontal;
    //垂直滚动条
    ScrollBar vertical;


    private ObservableList<Tab> tabs;

    //当前Tab
    Tab currentTab;
    //当前绘图面板
    private Pane drawPane;
    //当前结点
    private MindMapNode currentNode;
    //当前思维导图类
    private MindMap currentMindMap;
    //思维导图暂存区
    private Map<Integer,MindMap> tempOfMindMaps = new HashMap<>();

    private SingleSelectionModel<Tab> selectionModel;

    private Position position = new Position();

    private boolean flag = false;

    private MindMapNode center;


    @FXML
    private void initialize(){
        //水平滚动条
        horizontal = new ScrollBar();
        horizontal.setOrientation(Orientation.HORIZONTAL);
        horizontal.setPrefWidth(956);
        horizontal.setLayoutY(660);
        horizontal.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                drawPane.setTranslateX(-newValue.doubleValue()*10);
            }
        });
        //垂直滚动条
        vertical = new ScrollBar();
        vertical.setPrefHeight(630);
        vertical.setOrientation(Orientation.VERTICAL);
        vertical.setLayoutX(944);
        vertical.setLayoutY(30);
        vertical.valueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                drawPane.setTranslateY(-newValue.doubleValue()*10);
            }
        });

        mainPane.getChildren().add(vertical);
        mainPane.getChildren().add(horizontal);
        tabPane.setPrefWidth(2000);
        tabPane.setPrefHeight(2000);
        tabs = tabPane.getTabs();
        selectionModel = tabPane.getSelectionModel();
        tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
        //打开程序默认创建出一个空导图
            createMap();
        //loadTree  加载树节点
        loadTree(currentMindMap);
        //弹窗初始化
        dialogMess.setFont(new Font("YouYuan",15));
        dialogMess.setPrefWidth(150);
        dialogMess.setPrefHeight(60);
        dialogMess.setTranslateX(60);
        dialogMess.setTranslateY(20);
        dialogPane.getChildren().add(dialogMess);
        dialogStage = new Stage();
        dialogStage.setTitle("提示框");
        dialogStage.initModality(Modality.APPLICATION_MODAL);
        dialogStage.initOwner(primaryStage);
        Scene dialogScene = new Scene(dialogPane,250,100);
        dialogStage.setScene(dialogScene);
        //design  初始化界面样式
        design();
        dialog.setVisible(false);
        imageDialog.setVisible(false);
        if (tabs.size()>1){
            tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS);
        }else{
            tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
        }

        //加载图标及图标样式
        Image image = new Image("file:resources/createNode.jpg");
        Tooltip tip = new Tooltip("新建节点");
        tip.setFont(new Font("YouYuan", 16));
        Tooltip.install(createNode,tip);
        createNode.setImage(image);

        image = new Image("file:resources/storeMap.jpg");
        tip = new Tooltip("保存");
        tip.setFont(new Font("YouYuan", 16));
        Tooltip.install(storeMap,tip);
        storeMap.setImage(image);

        image = new Image("file:resources/deleteNode.jpg");
        tip = new Tooltip("删除节点");
        tip.setFont(new Font("YouYuan", 16));
        Tooltip.install(deleteNode,tip);
        deleteNode.setImage(image);

        image = new Image("file:resources/pre.jpg");
        tip = new Tooltip("上一步");
        tip.setFont(new Font("YouYuan", 16));
        Tooltip.install(pre,tip);
        pre.setImage(image);

        image = new Image("file:resources/next.jpg");
        tip = new Tooltip("下一步");
        tip.setFont(new Font("YouYuan", 16));
        Tooltip.install(next,tip);
        next.setImage(image);

        image = new Image("file:resources/imageToJPG.jpg");
        tip = new Tooltip("导出为JPG");
        tip.setFont(new Font("YouYuan", 16));
        Tooltip.install(imageToJPG,tip);
        imageToJPG.setImage(image);

        image = new Image("file:resources/rightLayout.jpg");
        tip = new Tooltip("右布局");
        tip.setFont(new Font("YouYuan",16));
        Tooltip.install(rightLayout,tip);
        rightLayout.setImage(image);

        image = new Image("file:resources/leftLayout.jpg");
        tip = new Tooltip("左布局");
        tip.setFont(new Font("YouYuan",16));
        Tooltip.install(leftLayout,tip);
        leftLayout.setImage(image);

        image = new Image("file:resources/autoLayout.jpg");
        tip = new Tooltip("自动布局");
        tip.setFont(new Font("YouYuan",16));
        Tooltip.install(autoLayout,tip);
        autoLayout.setImage(image);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        //主界面初始化
        this.primaryStage = primaryStage;
        URL cssUrl = getClass().getResource("../CSS/primaryStage.css");
        AnchorPane root = FXMLLoader.load(getClass().getResource("../FXML/MainInterface.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(cssUrl.toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("思维导图");
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    public void design(){
        //新建
        Label create = new Label("新建");
        create.setOnMouseClicked(event -> {
            createMap();
        });
        createNewMap.setGraphic(create);
        //打开
        Label open = new Label("打开");
        open.setOnMouseClicked(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setInitialDirectory(new File(PropertiesUtils.getStoreFilePath()));
            File targetFile = fileChooser.showOpenDialog(primaryStage);
            if(targetFile!=null) {
                MindMap target = FileUtils.getMindMap(targetFile);
                initOfMindMap(target);
                tempOfMindMaps.put(target.getId(), target);
                currentMindMap = target;
                openMindMap(currentMindMap);
            }
        });
        openMap.setGraphic(open);
        //新增
        createNode.setOnMouseMoved(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                createNode.setCursor(Cursor.HAND);
            }
        });


        createNode.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {

                if(currentNode==null){
                    dialogMess.setText("请先选中一个节点！");
                    dialogStage.show();
                    return;
                }

                if(currentNode.isCenter()) {
                    createChildNode();
                    LayoutUtils.layoutOfRight(MainInterface.this.center);
                    loadTree(currentMindMap);
                }else{
                    dialog.setVisible(true);
                }

            }
        });

        //保存
        storeMap.setOnMouseMoved(event -> {storeMap.setCursor(Cursor.HAND);});
        storeMap.setOnMouseClicked(event -> {
            try {
                packageOfMindMap();
                FileUtils.storeMindMap(currentMindMap);
                dialogMess.setText("导图保存成功！");
                dialogStage.show();
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        //删除
        deleteNode.setOnMouseMoved(event -> {deleteNode.setCursor(Cursor.HAND);});
        deleteNode.setOnMouseClicked(event -> {
            if(currentNode==null){
                dialogMess.setText("请先选中一个节点！");
                dialogStage.show();
                return;
            }
            if(!currentNode.isCenter()) {
                MindMapNode p = currentNode.getParentNode();
                TreeUtils.deleteNode(currentNode);
                removeLine(currentNode);
                Label id = (Label) drawPane.getChildren().get(drawPane.getChildren().size()-1);
                drawPane.getChildren().clear();
                drawPane.getChildren().add(id);
                reloadMap();
                loadTree(currentMindMap);
                currentMindMap.createSnapshotOfReback();
                currentNode = null;
            }else {
                dialogMess.setText("中心节点无法删除！");
                dialogStage.show();
            }
        });
        //上一步
        pre.setOnMouseMoved(event -> {
            pre.setCursor(Cursor.HAND);
        });
        pre.setOnMouseClicked(event ->  {
            currentMindMap.getSnapshotOfReback();
            Label id = (Label) drawPane.getChildren().get(drawPane.getChildren().size()-1);
            drawPane.getChildren().clear();
            drawPane.getChildren().add(id);
            reloadMap();
            loadTree(currentMindMap);
            ArrayList<MindMapNode> nodes = currentMindMap.getCenter().getChildNode();
            for(MindMapNode n : nodes){
                initAllNodes(n);
            }
        });
        //下一步
        next.setOnMouseMoved(event -> {next.setCursor(Cursor.HAND);});
        next.setOnMouseClicked(event -> {
            if(currentMindMap.getSnapshotOfNext()) {
                Label id = (Label) drawPane.getChildren().get(drawPane.getChildren().size() - 1);
                drawPane.getChildren().clear();
                drawPane.getChildren().add(id);
                reloadMap();
                loadTree(currentMindMap);
                ArrayList<MindMapNode> nodes = currentMindMap.getCenter().getChildNode();
                for (MindMapNode n : nodes) {
                    initAllNodes(n);
                }
                currentMindMap.createSnapshotOfReback();
            }
        });
        //tabs
        tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
            @Override
            public void changed(ObservableValue<? extends Tab> observable, Tab oldValue, Tab newValue) {
                drawPane = (Pane) newValue.getContent();
                Label id = (Label) drawPane.getChildren().get(drawPane.getChildren().size()-1);
                currentMindMap = tempOfMindMaps.get(Integer.parseInt(id.getText()));
                loadTree(currentMindMap);
            }
        });
        tabs.addListener(new ListChangeListener<Tab>() {
            @Override
            public void onChanged(Change<? extends Tab> c) {
                if (tabs.size()>1){
                    tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS);
                }else{
                    tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
                }
            }
        });
        //导出为JPG图片
        imageToJPG.setOnMouseMoved(event -> {imageToJPG.setCursor(Cursor.HAND);});
        imageToJPG.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                imageDialog.setVisible(true);
            }
        });

        //右布局
        rightLayout.setOnMouseMoved(event -> {rightLayout.setCursor(Cursor.HAND);});
        rightLayout.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                    LayoutUtils.layoutOfRight(MainInterface.this.center);
            }
        });

        //左布局
        leftLayout.setOnMouseMoved(event -> {leftLayout.setCursor(Cursor.HAND);});
        leftLayout.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                    LayoutUtils.layoutOfLeft(MainInterface.this.center);
            }
        });

        //自动布局
        autoLayout.setOnMouseMoved(event -> autoLayout.setCursor(Cursor.HAND));
        autoLayout.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                LayoutUtils.layoutOfAuto(MainInterface.this.center);
            }
        });

        list.setEditable(true);
        list.setCellFactory(TextFieldTreeCell.forTreeView(new StringConverter<String>() {
            public String toString(String obj){
                return obj;
            }
            public String fromString(String obj){
                return obj;
            }

        }));
    }

    //打开一份思维导图并设置样式并展示在页面上
    private void openMindMap(MindMap target){
            Tab tab = new Tab(target.getCenter().getMtext());
            tabs.add(tab);
            currentTab = tab;
            Pane pane = new Pane();
            pane.setPrefWidth(2000);
            pane.setPrefHeight(2000);
            tab.setContent(pane);
            Label id = new Label(String.valueOf(target.getId()));
            id.setVisible(false);
            id.setManaged(false);
            pane.getChildren().add(id);
            drawPane = pane;
            addActionOfDrawPane();
            MindMapNode center = target.getCenter();
            this.center = center;
            initWidth(center);
            addActionOfTabText(center);
            drawPane.getChildren().add(0,center);
            for(MindMapNode node : center.getChildNode()){
                loadNodes(node);
            }
            for(MindMapLine line : target.getLines()){
             drawPane.getChildren().add(0,line);
            }
            selectionModel.select(tab);
            currentMindMap.createSnapshotOfReback();
    }

    //加载所有节点并初始化样式
    private void loadNodes(MindMapNode node){
        for(MindMapNode m : node.getChildNode()){
            loadNodes(m);
        }
        drawPane.getChildren().add(0,node);
        initWidth(node);
    }

    //创建新导图
    private void createMap(){
        Tab tab = new Tab("demo");
        currentTab = tab;
        tabs.add(tab);
        Pane pane = new Pane();
        pane.setPrefWidth(2000);
        pane.setPrefHeight(2000);
        tab.setContent(pane);
        drawPane = pane;
        MindMap map = new MindMap(getId());
        Label id = new Label(String.valueOf(map.getId()));
        id.setVisible(false);
        id.setManaged(false);
        pane.getChildren().add(id);
        currentMindMap = map;
        addActionOfDrawPane();
        createCenterNode();
        tempOfMindMaps.put(map.getId(),map);
        selectionModel.select(tab);
        currentMindMap.createSnapshotOfReback();
    }

    //给绘制面板添加点击事件
    private void addActionOfDrawPane(){
        drawPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(currentNode!=null) {
                    removeActionOfNode();
                }
                    currentNode = null;
                dialog.setVisible(false);
                imageDialog.setVisible(false);
            }
        });
    }

    //创建中心节点
    private void createCenterNode(){
        MindMapNode center = new MindMapNode("中心节点");
        this.center = center;
        currentMindMap.setCenter(center);
        center.setEditable(false);
        center.setId("center-node");
        center.setPrefWidth(110);
        center.setPrefHeight(40);
        center.setLayoutY(240);
        center.setLayoutX(600);
        center.setCenter(true);
        initNode(center);
        addActionOfTabText(center);
        drawPane.getChildren().add(0,center);
    }

    //移除节点的编辑事件
    private void removeActionOfNode(){
        currentNode.setEditable(false);
        initNode(currentNode);
        currentNode.setStyle("-fx-border-color:black");
    }

    //初始化节点的各种鼠标事件
    private void initNode(MindMapNode node){

        node.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if(newValue.length()>6) {
                    node.setPrefWidth(110 + (newValue.length() - 6) * 15);
                    node.setText(newValue);
                }else{
                    node.setPrefWidth(110);
                }
            }
        });

        node.setOnMousePressed(event1 -> {
            if(currentNode!=null){
                removeActionOfNode();
            }
            currentNode = node;
            currentNode.setStyle("-fx-border-color:#3399ff");
            currentNode.setCursor(Cursor.MOVE);
            Position.x = event1.getX();
            Position.y = event1.getY();
        });

        node.setOnMouseReleased(event1 -> {
            node.setCursor(Cursor.DEFAULT);
            if(flag) {
                currentMindMap.createSnapshotOfReback();
                flag = false;
            }
        });


        node.setOnMouseDragged(event1 -> {
            double distanceX = event1.getX() - Position.x;
            double distanceY = event1.getY() - Position.y;
            double x = node.getLayoutX() + distanceX;
            double y = node.getLayoutY() + distanceY;
            node.relocate(x,y);
            flag = true;
        });

        node.setOnMouseClicked(event1 -> {
            if(event1.getClickCount() == 2){
                node.setOnMousePressed(null);
                node.setOnMouseDragged(null);
                node.setEditable(true);
            }
        });
    }

    private void packageOfMindMap(){
            for(MindMapLine line : currentMindMap.getLines()){
                line.setMBeginX(line.getBeginX());
                line.setMBeginY(line.getBeginY());
                line.setMEndX(line.getEndX());
                line.setMEndY(line.getEndY());
            }
            MindMapNode c = currentMindMap.getCenter();
            c.setMtext(c.getTextProperty());
            c.setMx(c.getX());
            c.setMy(c.getY());
            for(MindMapNode n : c.getChildNode()){
                packOfMindMapNodes(n);
            }
    }

    private void initOfMindMap(MindMap target){
            MindMapNode c = target.getCenter();
            c.setTextProperty(c.getMtext());
            c.setX(c.getMx());
            c.setY(c.getMy());
            initNode(c);
            c.setId("center-node");
            c.setPrefWidth(110);
            c.setPrefHeight(40);
            c.init(c.getMtext());
            for(MindMapNode n : c.getChildNode()){
                initOfMindMapNodes(n);
            }
            for(MindMapLine line : target.getLines()){
                line.init(line.getBeginNode(),line.getEndNode(),line.getMBeginX()+line.getBeginNode().getPrefWidth()/2,line.getMBeginY()+line.getBeginNode().getPrefHeight()/2,line.getMEndX(),line.getMEndY());
            }
                tempOfMindMaps.put(target.getId(),target);
    }

    private void packOfMindMapNodes(MindMapNode node){
        for(MindMapNode n : node.getChildNode()){
            packOfMindMapNodes(n);
        }
        node.setMtext(node.getTextProperty());
        node.setMx(node.getX());
        node.setMy(node.getY());
    }

    private void initOfMindMapNodes(MindMapNode node){
        for(MindMapNode n : node.getChildNode()){
            initOfMindMapNodes(n);
        }
        node.setTextProperty(node.getMtext());
        node.setX(node.getMx());
        node.setY(node.getMy());
        node.init(node.getMtext());
        node.setId("node");
        node.setPrefWidth(110);
        node.setPrefHeight(40);
        initNode(node);
    }

    private void addActionOfTabText(MindMapNode node){
            node.getTextOfProperty().bindBidirectional(currentTab.textProperty());
    }

    private Integer getId(){
        return (int)(Math.random()*10000);
    }

    private void initWidth(MindMapNode node){
        if(node.getText().length()>6) {
            node.setPrefWidth(110 + (node.getText().length() - 6) * 15);
        }else{
            node.setPrefWidth(110);
        }
    }

    @FXML
    private void createChildNode(){
        dialog.setVisible(false);
        MindMapNode node = new MindMapNode("子节点");
        node.setParentNode(currentNode);
        MindMapLine line = new MindMapLine(currentNode,node,currentNode.getLayoutX()+currentNode.getPrefWidth()/2,currentNode.getLayoutY()+currentNode.getPrefHeight()/2,node.getLayoutX()+node.getPrefWidth()/2,node.getLayoutY()+node.getPrefHeight()/2);
        node.setEditable(false);
        node.setId("node");
        node.setCenter(false);
        node.setPrefWidth(110);
        node.setPrefHeight(40);
        initNode(node);
        initXAndYOfChildNode(node);
        drawPane.getChildren().add(0, node);
        drawPane.getChildren().add(0,line);
        currentNode.getChildNode().add(node);
        currentMindMap.getLines().add(line);
        currentMindMap.createSnapshotOfReback();
        loadTree(currentMindMap);
        LayoutUtils.layoutOfRight(this.center);
    }

    @FXML
    private void createSiblingNode(){
        dialog.setVisible(false);
        MindMapNode node = new MindMapNode("子节点");
        node.setParentNode(currentNode.getParentNode());
        MindMapLine line = new MindMapLine(currentNode.getParentNode(),node,currentNode.getParentNode().getLayoutX()+50,currentNode.getParentNode().getLayoutY()+20,node.getLayoutX()+50,node.getLayoutY()+20);
        node.setEditable(false);
        node.setId("node");
        node.setCenter(false);
        node.setPrefWidth(110);
        node.setPrefHeight(40);
        initNode(node);
        initXAndYOfSiblingNode(node);
        drawPane.getChildren().add(0, node);
        drawPane.getChildren().add(0,line);
        currentNode.getParentNode().getChildNode().add(node);
        currentMindMap.getLines().add(line);
        currentMindMap.createSnapshotOfReback();
        loadTree(currentMindMap);
        LayoutUtils.layoutOfRight(this.center);
    }

    @FXML
    private void imageToJPG(){
        WritableImage image = drawPane.snapshot(new SnapshotParameters(),null);
        File file = new File(PropertiesUtils.getJPGFilePath());
        if(!file.exists()){
            file.mkdirs();
        }
        try{
            ImageIO.write(SwingFXUtils.fromFXImage(image,null),"png",new File(PropertiesUtils.getJPGFilePath()+"//"+currentMindMap.getCenter().getTextProperty()+".jpg"));
            dialogMess.setText("已导出为JPG图片！");
            dialogStage.show();
        }catch (Exception e){
            e.printStackTrace();
        }
        imageDialog.setVisible(false);
    }

    @FXML
    private void imageToPNG(){
        WritableImage image = drawPane.snapshot(new SnapshotParameters(),null);
        File file = new File(PropertiesUtils.getJPGFilePath());
        if(!file.exists()){
            file.mkdirs();
        }
        try{
            ImageIO.write(SwingFXUtils.fromFXImage(image,null),"png",new File(PropertiesUtils.getJPGFilePath()+"//"+currentMindMap.getCenter().getTextProperty()+".png"));
            dialogMess.setText("已导出为PNG图片！");
            dialogStage.show();
        }catch (Exception e){
            e.printStackTrace();
        }
        imageDialog.setVisible(false);
    }

    private void loadTree(MindMap currentMindMap){
            TreeItem root = new TreeItem(currentMindMap.getCenter().getTextProperty());
            root.valueProperty().bindBidirectional(currentMindMap.getCenter().getTextOfProperty());
            root.setExpanded(true);
            root.valueProperty().bindBidirectional(currentMindMap.getCenter().getTextOfProperty());
            list.setRoot(root);
            loadTreeNode(root,currentMindMap.getCenter());
    }

    private void loadTreeNode(TreeItem currentNode,MindMapNode currentMindMapNode){
        ArrayList<MindMapNode> childNodes = currentMindMapNode.getChildNode();
        if(childNodes.size()==0){
            return;
        }
        for(int i = 0 ; i<childNodes.size() ; i++){
            TreeItem item = new TreeItem(childNodes.get(i).getTextProperty());
            item.valueProperty().bindBidirectional(childNodes.get(i).getTextOfProperty());
            item.setExpanded(true);
            item.valueProperty().bindBidirectional(childNodes.get(i).getTextOfProperty());
            currentNode.getChildren().add(item);
            loadTreeNode(item,childNodes.get(i));
        }
    }

    private void reloadMap(){
        MindMapNode center = currentMindMap.getCenter();
        drawPane.getChildren().add(0,center);
        for(MindMapNode node : center.getChildNode()){
            loadNodes(node);
        }
        for(MindMapLine line : currentMindMap.getLines()){
            drawPane.getChildren().add(0,line);
        }
    }

    private void removeLine(MindMapNode target){
        ArrayList lines = currentMindMap.getLines();
        for(int i = 0;i<lines.size();i++){
            MindMapLine line = (MindMapLine) lines.get(i);
            if(line.getBeginNode()==target||line.getEndNode()==target){
                lines.remove(line);
                i--;
            }
        }
        removeAllLines(target);
    }

    private void removeAllLines(MindMapNode target){
        ArrayList childNodes = target.getChildNode();
        ArrayList lines = currentMindMap.getLines();
        for(int i = 0;i<childNodes.size();i++){
            removeAllLines((MindMapNode) childNodes.get(i));
        }
        for(int j = 0;j<lines.size();j++){
            MindMapLine line = (MindMapLine)lines.get(j);
            if(line.getBeginNode()==target||line.getEndNode()==target){
                lines.remove(line);
                j--;
            }
        }
    }

    private void initAllNodes(MindMapNode node){
        ArrayList<MindMapNode> nodes = node.getChildNode();
        for(MindMapNode n : nodes){
            initAllNodes(n);
        }
        node.setEditable(false);
        node.setId("node");
        node.setCenter(false);
        node.setPrefWidth(110);
        node.setPrefHeight(40);
        initNode(node);
    }

    private void initXAndYOfChildNode(MindMapNode node){
        if(currentNode.isCenter()){
            if(currentNode.getChildNode().size()==0) {
                node.setLayoutX(currentNode.getX()-200);
                node.setLayoutY(currentNode.getY()-120);
                return;
            }
            node.setLayoutX(currentNode.getChildNode().get(currentNode.getChildNode().size()-1).getX());
            node.setLayoutY(currentNode.getChildNode().get(currentNode.getChildNode().size()-1).getY()+60);
        }
        if(currentNode.getChildNode().size()==0){
            node.setLayoutX(currentNode.getX()-200);
            node.setLayoutY(currentNode.getY()-120);
            return;
        }
        node.setLayoutX(currentNode.getChildNode().get(currentNode.getChildNode().size()-1).getX());
        node.setLayoutY(currentNode.getChildNode().get(currentNode.getChildNode().size()-1).getY()+60);
    }

    private void initXAndYOfSiblingNode(MindMapNode node){
        MindMapNode parent = node.getParentNode();
        node.setLayoutX(parent.getChildNode().get(parent.getChildNode().size()-1).getX());
        node.setLayoutY(parent.getChildNode().get(parent.getChildNode().size()-1).getY()+60);
    }
}
